package test.处理;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import test.匹配pmid图片相关.CommonTable;
import utils.SmallTool;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

import static test.处理.比对老板网页数据单独比对出版时间20220514.preDealStr;

/**
 * @Author: gcr
 * @Date: 2022-5-17 20:13
 */
public class 输出老板全部准确的pm发布年 {

    @SneakyThrows
    public static void main(String[] args) {

        String dbNameMy = CommonTable.pmiddoiFromBossHtml;
        String ins = "gcr_pm_pub_year";

        int breakSize = 100000;

        SmallTool.printMemory();
        DataSource ds = DSFactory.get("server_local3306");
        Db use3307 = Db.use(ds);
        String allDate = "select max(pmid) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));

        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMemory();
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始页码++" + i + "++id++" + start + "结束id++++" + end);
            String meSql = "select text,pmid  from " + dbNameMy + " where pmid between " + start + " and " + end;
            List<Entity> queryMe = use3307.query(meSql);
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer pmid = pmDeatil.getInt("pmid");
                String text = pmDeatil.getStr("text");
                Entity where = new Entity();
                where.setTableName(ins);
                where.set("pmid", pmid);
                where.set("text", text);
                String boosNian = "";
                String ISSusBoss = preDealStr(text);
                if (ISSusBoss != null) {
                    //情况太多来的 值匹配年和和后面的数据 其他不看了
                    String[] splitBoss = ISSusBoss.split(";");
                    String splitBoss1 = splitBoss[0];

                    String[] s1 = splitBoss1.split(" ");
                    if (s1.length > 1) {
                        try {
                            boosNian = s1[1];
                            Integer.parseInt(boosNian);
                            if (boosNian.length() != 4) {
                                boosNian = s1[0];
                                Integer.parseInt(boosNian);
                            }
                        } catch (Exception e) {
                            boosNian = s1[0];
                        }

                    } else {
                        boosNian = s1[0].trim();
                    }
                }
                where.set("year", boosNian);
                try {
                    use3307.insert(where);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            });

        }


    }
}
